package com.codi.fcloud.ufx.hssale.trade;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.ExceptionUtil;
import com.codi.bus.exception.HundsunAPIException;
import com.codi.fcloud.ufx.HsUtil;
import com.codi.fcloud.ufx.UfxSDKFactory;
import com.codi.fcloud.ufx.constant.HSInputParameterNames;
import com.codi.fcloud.ufx.constant.UrlConstants;
import com.codi.trade.dto.HsDividendMethodTradeRequestDto;
import com.codi.trade.response.HsDividendMethodTradeResponse;
import com.codi.trade.service.HsDividendMethodTradeService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 模块名
 *
 * @author spy
 * @version 1.0 2018-01-25
 * @since 1.0
 */
@Slf4j
@Service
public class HsDividendMethodTradeServiceImpl extends UfxSDKFactory implements HsDividendMethodTradeService {
    @Override
    protected String getTarget() {
        return UrlConstants.PREFIX_SALE + UrlConstants.DIVIDEND_METHOD_TRADE;
    }

    @Loggable
    @Override
    public HsDividendMethodTradeResponse change(HsDividendMethodTradeRequestDto dto) {
        Preconditions.checkNotNull(dto);

        String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);

        Map<String, Object> request = new HashMap<>();

        request.put("trust_way", trustWay); //	C1	交易委托方式	Y
        request.put("net_no", getString(dto.getNet_no(), DEF_NET_NO)); //	C9	网点代码	N	不传则使用默认值"0001" ，有传值（包括空）则使用所传值
        request.put("trade_acco", getString(dto.getTrade_acco())); //	C17	交易账号	Y
        request.put("ta_acco", getString(dto.getTa_acco())); //	C12	TA账号	Y
        request.put("password", getString(dto.getPassword())); //	C50	密码	Y	传加密后的密文,默认MD5(加密后密文是32位)加密方式
        request.put("apply_time", getString(dto.getApply_time())); //	N6	申请时间	N
        request.put("order_date", getString(dto.getOrder_date())); //	N8	下单日期	N	为空则下单日期取服务器收到报文的请求日期；不为空则该日期为下单日期；
        request.put("fund_code", getString(dto.getFund_code())); //	C6	基金代码	Y
        request.put("share_type", getString(dto.getShare_type())); //	C1	份额分类	Y
        request.put("auto_buy", getString(dto.getAuto_buy())); //	C1	分红方式	Y	0-红利再投资，1-现金红利
        request.put("trade_conduct_person", getString(dto.getTrade_conduct_person())); //	C20	交易经办人	N
        request.put("channel_serial_id", getString(dto.getChannel_serial_id())); //	C32	银商流水号	N
        request.put("trade_source", getString(dto.getTrade_source())); //	C12	交易来源	N


        try {
            //
            Map<String, Object> response = getUfxSDK().transact(getTarget(), request);

            return HsUtil.convertMap(appendLog(request, response), HsDividendMethodTradeResponse.class);

        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }

    }
}
